/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"8F2D7F25-AAA5-4639-B34C-49E1ABFC86A4",variableType:4}
 */
var es_preview = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"50E98309-F027-4B07-A040-9939249EE1F3",variableType:4}
 */
var fSucursal = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"1FA529DC-5458-492C-84B9-6CDE33D66F38",variableType:4}
 */
var fNroFactura = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"4A7AE06C-B25D-4C65-8B8F-DC9DA64E7E02"}
 */
var fLetra = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"A8438B24-2AA2-4F78-B9E3-35A67D2FE4FA",variableType:4}
 */
var fRemNro = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"F7696A85-1CA0-4ACA-80B5-91F328A491D3",variableType:4}
 */
var fRemSucursal = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"B98AD942-AC5C-43F1-8F6E-CAEB24AFE654",variableType:8}
 */
var fTotKg = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"ED119AD4-5926-46CB-883E-E81EA7E62877",variableType:8}
 */
var fTotP2 = 0;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"CC03E032-5E4D-43B1-A0AE-2A737F6ACAFD",variableType:8}
 */
var fTotRecep = 0;

/**
 * Handle changed data.
 *
 * @param {Object} [oldValue] old value
 * @param {Object} [newValue] new value
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"9E2E9008-C029-4E3E-9F41-2E74D525B76C"}
 */
function onDataChangeTipo(oldValue, newValue, event) {
	elements.tabs_180.visible = true
	if (tipo == 0)
	{
		elements.grp_cliente.visible	= 0
		elements.grp_proveedor.visible 	= 1
		elements.tabs_180.tabIndex		= 2
		cliente_id = null
	}
	else
		if (tipo == 1)
		{
			elements.grp_cliente.visible	= 1
			elements.grp_proveedor.visible 	= 0
			elements.tabs_180.tabIndex		= 1
			proveedor_id = null
		}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"A500AE92-C0D3-4450-84A7-821D921B04C9"}
 */
function buscarDocumento(event) {
	if (proveedor_id)
	{
		globals.formEnVentana('recepciones_oc_dlg','BUSCADOC','Buscar Orden de Compra')
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','Debe elegir un proveedor primero','OK')
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} [firstShow] form is shown first time after load
 * @param {JSEvent} [event] the event that triggered the action
 *
 * @properties={typeid:24,uuid:"695D177B-6ACF-4FE0-AC13-6A3C50353562"}
 */
function onShow(firstShow, event) {
	//Por parametrizacion de los forms dentro del tab panel
	forms.recepciones_oc_tab.fFormLlama 		= 'recepciones_carga_cab'
	forms.recepciones_carga_rem_tab.fFormLlama 	= 'recepciones_carga_cab'
	forms.recepciones_oc_dlg.fFormLlama			= 'recepciones_carga_cab'
	globals.gProveedorRecep = null
	
	databaseManager.setAutoSave(false)
	controller.newRecord()
	deposito	= globals.gdeposito
	fecha		= application.getServerTimeStamp()
	application.setValueListItems('recepciones_oc_detalle_din',new Array())
	es_preview 	= 0
	
	fTotRecep 	= 0
	fTotKg 		= 0
	fTotP2 		= 0
	
	fRemNro 		= null
	fRemSucursal 	= null
	fNroFactura 	= null
	fSucursal 		= null
	fLetra 			= null
		
	tipo = 0
	onDataChangeTipo()
	for (var i = 1 ; i <= 10 ; i++)
	{
		recep_cabecera_to_recep_detalle.newRecord()
	}
}

/**
 * @properties={typeid:24,uuid:"E9C3CEA5-C02A-47BE-8049-A797C9E43A5E"}
 */
function confirmar() {

	if (deposito == null)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor elija un deposito para seguir','OK')
		return
	}

	if (nro_remito == null)
	{
		globals.DIALOGS.showWarningDialog('Aviso','Por favor indique un remito para seguir','OK')
		return
	}
	
	var $tot_recep 	= 0
	
	if (tipo == 0)
	{// Proveedor - Orden de Compra
		var $tot_oc		= 0
		var $tot_actual	= 0
		
		var i = 1 ; 
		while (i <= recep_cabecera_to_recep_detalle.getSize())
		{
			var $rcd = recep_cabecera_to_recep_detalle.getRecord(i)
			
			if ($rcd.cod_producto == null)
			{
				recep_cabecera_to_recep_detalle.deleteRecord(i)
			}
			else
			{	
				$tot_recep 	+= $rcd.total_renglon
				if ($rcd.recep_detalle_to_oc_detalle != null)
				{
					$tot_actual += $rcd.precio_actual * $rcd.cantidad_alter
					$tot_oc 	+= $rcd.precio_sistema_oc * $rcd.cantidad_alter	
				}
				i++
			}
		}
		//Saco la diferencia entre el precio al q se compro y el precio actual
		var $dif = $tot_actual - $tot_oc
		application.output($dif)
		razon_social = recep_cabecera_to_proveedor.razon_social
	}
	else
	{//Cliente - Remito
		
		var i = 1;
		while (i <= recep_cabecera_to_recep_detalle.getSize())
		{
			var $rcd 			= recep_cabecera_to_recep_detalle.getRecord(i)
			
			if ($rcd.cod_producto == null)
			{
				recep_cabecera_to_recep_detalle.deleteRecord(i)
			}
			else
			{
				$rcd.total_renglon 	= $rcd.cantidad * $rcd.precio_unitario
				$tot_recep 			+= $rcd.total_renglon
				if ($rcd.recep_detalle_to_rem_detalle.cantidad_pendiente_fac > $rcd.cantidad_alter)
					$rcd.recep_detalle_to_rem_detalle.cantidad_pendiente_fac -= $rcd.cantidad_alter
				else
				{
					$rcd.recep_detalle_to_rem_detalle.cantidad_pendiente_fac = 0
				}
				i++
			}
		}
		razon_social = recep_cabecera_to_clientes.razon_social
	}
	recep_estado = 2

	fTotRecep 	= $tot_recep
	total		= $tot_recep
	
	recepciones_to_numeracion_pr.ultimo_numero++
	nro_recep = recepciones_to_numeracion_pr.ultimo_numero
	
	if (databaseManager.saveData())
	{
		if (globals.DIALOGS.showQuestionDialog('Imprime','Recepcion Nº '+ nro_recep+'.¿Desea imprimirla?','Si','No') == 'Si')
		{
			databaseManager.setAutoSave(true)
			es_preview = 1
			//forms.recepciones_cab_imp.buscarTotalesMadera()
			forms.recepciones_cab_imp.controller.loadRecords(recep_cabecera_id)
			forms.recepciones_cab_imp.controller.showPrintPreview()
			
/*			var $params = {
				pFacturaId: recep_cabecera_id,
				pRazonSocial: uservs_to_uservs.razon_social,
				pTipo: application.getValueListDisplayValue('recepciones_tipos',tipo)
			}
			globals.metodoJasper('impresion','proveedores/recepcion.jasper','Recepcion','infocom',$params)*/
		}
		else
			onShow()
	}
	else
		globals.DIALOGS.showWarningDialog('Aviso','Ocurrio un error en la grabacion','OK')
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"629A0E11-7B74-453F-8EDA-D313881A0D00"}
 */
function eliminar(event) {
	recep_cabecera_to_recep_detalle.deleteRecord()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"C0C8CAFC-B9DD-4BAB-9B40-4D1D9C4F4C10"}
 */
function agregar(event) {
	recep_cabecera_to_recep_detalle.newRecord(false)
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7BFC4BF8-45AD-4BD8-9FD3-15BF6693691A"}
 */
function cancelar(event) {
	databaseManager.revertEditedRecords()
	onShow()
}

/**
 * @properties={typeid:24,uuid:"D3E0CF80-6CE4-4808-A1DE-3E153386C28E"}
 */
function calcularTotales()
{
	total  = 0
	for (var i = 1 ; i <= recep_cabecera_to_recep_detalle.getSize() ; i++)
	{
		var $rcd 			= recep_cabecera_to_recep_detalle.getRecord(i)
		
		if ($rcd.cod_producto != null)
		{
			$rcd.total_renglon 	= $rcd.cantidad * $rcd.precio_unitario
			total += $rcd.total_renglon
		}
	}
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"A2BA109C-A30E-4DD4-9A35-0B0D17B53BF8"}
 */
function onDataChangeProveedor(oldValue, newValue, event) {
	globals.gProveedorRecep = proveedor_id
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"EAA9BF91-7701-4D32-83AD-5E732403A590"}
 */
function onDataChangeRemito(oldValue, newValue, event) {
	if (fRemNro != null && fRemSucursal != null)
	{
		nro_remito = 'X-'+fRemNro+'-'+fRemSucursal
		
		var args = [nro_remito, proveedor_id]
		var query = 'select count(recep_cabecera_id) from recep_cabecera where nro_remito = ? and proveedor_id = ? group by recep_cabecera_id'
		var ds = databaseManager.getDataSetByQuery('infocom',query,args,-1)
		
		var $ctos = ds.getValue(1,1)
		if ($ctos > 0)
		{
			globals.DIALOGS.showWarningDialog('Aviso','Número de remito duplicado','OK')
			return false
		}	
	}
	
	return true
}

/**
 * Handle changed data.
 *
 * @param {Object} oldValue old value
 * @param {Object} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"D9CE0528-0F84-4DC1-9BE6-1D829BAB3FF2"}
 */
function onDataChangeFactura(oldValue, newValue, event) {
	if (fLetra != null && fSucursal != null && fNroFactura != null)
		nro_factura = fLetra + '-' + fSucursal + '-' + fNroFactura
	else
		nro_factura = null
	return true
}
